import sys,matplotlib
from matplotlib import colors, ticker, cm
sys.path.append('../../utils/')
matplotlib.use('Agg')
from numpy import *
from pylab import *
import getopt
import os, re
import pdb
import math as math
import linecache
from matplotlib.colors import LogNorm
from scipy import interpolate
import pickle as pickle

############################################################################################ 

# SPECIFY FILES + SPECIES TO PLOT 

# left is denominator: kinmars_new.out-1
# right is numerator: swap that between kinmars_old, kinmars_old_1, and kinmars_changes
files = ['kinmars_changes_arthur_2_frank.out-1','kinmars_changes_check.out-1']
# ignore this ,'kinmars.out-1']

labels = ['frank kindata','replaced H2O file']
# ignore this 'our kindata']

# change the linestyle, always the first entry
lns = ['-.','--','-.',':'] # linestyle (solid, dash, dash-dot, dotted). One per filename

#pnames = ['CL','CL2','CLO','HCL','HOCL','CLCO','COCL2','CLCO3',\
#          'OCLO', 'CLO3', 'CL2O', 'CL2O2', 'CL2O3', 'CLNO','CLNO2','CLONO','CLNO3']
#pnames = ['CO','O2','H2','O3','NO','HNO3','OH','HO2','H']
#pnames = ['NO','NO2','NO3','HNO2','HNO3']
#pnames = ['OH','HO2','H2O2']
#pnames = ['CL','CL2','CLO','HCL','HOCL','CLCO3','CLNO','CLO3','CLNO3']
#pnames = ['O','O2','O3','CO']
pnames = ['CO','O2','H2','O3','NO','HNO3','OH','HO2','H']
 # can take more/less species, just make sure len(pnames) < len(pclr)
pclr = ['magenta','purple','navy','blue','orange','green','red','cyan','brown',\
        'magenta','purple','navy','blue','orange','green','red','cyan','brown'] # one color per species

############################################################################################

# Plot Style Things
matplotlib.rc('font', family='serif')
matplotlib.rc('xtick', labelsize = 25)
matplotlib.rc('ytick', labelsize = 25)
matplotlib.rc('axes', linewidth=2)
fontname = 'serif'
fontsize=30
lw = 3.3

# OPEN FIGURE 
fig,ax=plt.subplots(figsize=(12,12))

############################################################################################

# READ & PLOT FOR EACH FILE

# Get data from "files", loop over filenames in list
fctr = 0
mix = {}
for ifile in files:
    mix[ifile]={}
    infile = open(ifile,'r')
    temp = infile.readline()

    while not 'NATOM' in temp:
        temp = infile.readline()
    temp = infile.readline().split() # vals 
    natom, nmol, nreact, nfix, nvary = int(temp[0]), int(temp[1]), int(temp[2]), int(temp[4]), int(temp[6])
    temp = infile.readline() # header       
    temp = infile.readline().split() # vals 
    nz = int(temp[2])

    # skip all lines that come before 'MIXING RATIO'
    temp = infile.readline()
    while 'MIXING RATIO' not in temp:
        temp = infile.readline()
    
    # loop over groups of species 
    alt = np.zeros(nz)

    blank = infile.readline()
    header = infile.readline().split()
    alt = np.zeros(nz)
    while len(header)>1:
        for ix in range(1,len(header)):
            mix[ifile][header[ix]]= np.zeros(nz)
        for iz in range(0,nz):
            temp = infile.readline().split()
            for ix in range(1,len(header)):
                mix[ifile][header[ix]][iz] = float(temp[ix+1])
            alt[iz] = float(temp[1])
        blank = infile.readline()
        header = infile.readline().split()

for ipnames in range(0,len(pnames)):
    ax.plot(mix[files[1]][pnames[ipnames]]/mix[files[0]][pnames[ipnames]],alt,color=pclr[ipnames],linestyle=lns[fctr],linewidth=lw,label=pnames[ipnames])
ax.set_title('New/Old H2O Cross_sxns')    
# put legend at top of plot, use 5 columns to list everything
ax.legend(bbox_to_anchor=(0.5,1.1),ncol=5)

# axis limits 
ax.set_ylim([0,100])
ax.set_xlim([0.68,1.6])
ax.set_xlabel('Mixing Ratio',fontsize=fontsize,fontname=fontname)
ax.set_ylabel('Altitude',fontsize=fontsize,fontname=fontname)

plt.savefig('mixingOyCOyNOyHy_aug_25_updated.png')

# numerator, linestyle, and filename at the end !!!!
